/**
 * 
 */
package com.tpl.core.basedata.service.impl;

import java.util.List;

import com.tpl.core.basedata.dao.RoleDao;
import com.tpl.core.basedata.service.RoleUserService;
import com.tpl.core.dao.ExecuteSql;
import com.tpl.core.entity.sys.Role;
import org.springframework.beans.factory.annotation.Autowired;



/**
 * @author 狄敏
 *
 * 创建时间：Jul 30, 2013 4:26:53 PM
 */
public class RoleUserServiceImpl implements RoleUserService {

	@Autowired
	private RoleDao roleDao;
	
	@Override
	public void saveRoleUser(Long[] new_, Long[] del_, Long roleId) {
		//删除去掉的人员
		if(del_ != null && del_.length>0){
			for(int i=0;i<del_.length;i++){
				roleDao.deleteRoleUserById(del_[i]);
			}
		}

		//保存新增的人员
		if(new_ != null && new_.length > 0){
			for(int i=0;i<new_.length;i++){
				Role role = new Role();
				role.setUserId(new_[i]);
				role.setRoleId(roleId);
				roleDao.insertRoleUser(role);
			}
		}
	}

	@Override
	public void saveUserRole(Long[] new_, Long[] del_, Long userId) {
		//删除去除的角色
		if(del_ != null && del_.length > 0){
			for(int i=0;i<del_.length;i++){
				roleDao.deleteRoleUserById(del_[i]);
			}
		}
		//保存新增的角色
		if(new_ != null && new_.length > 0){
			for(int i=0;i<new_.length;i++){
				Role role = new Role();
				role.setUserId(userId);
				role.setRoleId(new_[i]);
				roleDao.insertRoleUser(role);
			}
		}
	}

	@Override
	public Long[] getRoleByUserRoleId(Long id) {
		String sql="select user_id,role_id from sys_user_re_role r where id="+id;
		List<Object[]> list= ExecuteSql.getFields(sql);
		if(!list.isEmpty()){
			Object[] rel=list.get(0);
			return new Long[]{Long.valueOf(rel[0].toString()),Long.valueOf(rel[1].toString())};
		}
		return new Long[2];
	}
}